//package com.zhixue.orders.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.alipay.api.AlipayApiException;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.internal.util.AlipaySignature;
//import com.alipay.api.request.AlipayTradeQueryRequest;
//import com.alipay.api.request.AlipayTradeWapPayRequest;
//import com.alipay.api.response.AlipayTradeQueryResponse;
//import com.alipay.api.response.AlipayTradeWapPayResponse;
//import com.zhixue.orders.config.AlipayConfig;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Iterator;
//import java.util.Map;
//
///**
// * @author Dragon
// * @description PayTestController
// * @date 2023/10/27 14:59
// */
//@Controller
//public class PayTestController {
//
//    @Value("${pay.alipay.APP_ID}")
//    String APP_ID;
//
//    @Value("${pay.alipay.APP_PRIVATE_KEY}")
//    String APP_PRIVATE_KEY;
//
//    @Value("${pay.alipay.ALIPAY_PUBLIC_KEY}")
//    String ALIPAY_PUBLIC_KEY;
//
//
//    /**
//     * 创建订单
//     * @param httpRequest
//     * @param httpResponse
//     * @return
//     * @throws ServletException
//     * @throws IOException
//     * @throws AlipayApiException
//     */
//    @RequestMapping("/alipaytest")
//    @ResponseBody
//    public String doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException, AlipayApiException {
//
//        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, APP_ID, APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
//        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
////异步接收地址，仅支持http/https，公网可访问
//        request.setNotifyUrl("https://54z23379y1.zicp.fun/orders/paynotify");
////同步跳转地址，仅支持http/https,不建议，就是通过重定向的方式，不能保证通知到位
////        request.setReturnUrl("");
///******必传参数******/
//        JSONObject bizContent = new JSONObject();
////商户订单号，商家自定义，保持唯一性
//        bizContent.put("out_trade_no", "202108170101010485140");
////支付金额，最小值0.01元
//        bizContent.put("total_amount", 10000);
////订单标题，不可使用特殊符号
//        bizContent.put("subject", "测试商品");
//
///******可选参数******/
////手机网站支付默认传值FAST_INSTANT_TRADE_PAY
//        bizContent.put("product_code", "QUICK_WAP_WAY");
////bizContent.put("time_expire", "2022-08-01 22:00:00");
//
////// 商品明细信息，按需传入
////JSONArray goodsDetail = new JSONArray();
////JSONObject goods1 = new JSONObject();
////goods1.put("goods_id", "goodsNo1");
////goods1.put("goods_name", "子商品1");
////goods1.put("quantity", 1);
////goods1.put("price", 0.01);
////goodsDetail.add(goods1);
////bizContent.put("goods_detail", goodsDetail);
//
////// 扩展信息，按需传入
////JSONObject extendParams = new JSONObject();
////extendParams.put("sys_service_provider_id", "2088511833207846");
////bizContent.put("extend_params", extendParams);
//
//        request.setBizContent(bizContent.toString());
//        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
//        if(response.isSuccess()){
//            System.out.println("调用成功");
//        } else {
//            System.out.println("调用失败");
//        }
//
//        return response.getBody();
//
//    }
//
//
////    @RequestMapping("/alipaytest")
////    public void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException, AlipayApiException {
////        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, APP_ID, APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
////        //获得初始化的AlipayClient
////        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
////        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
////        alipayRequest.setNotifyUrl("http://7veh8s.natappfree.cc/orders/paynotify");//在公共参数中设置回跳和通知地址
////        alipayRequest.setBizContent("{" +
////                "    \"out_trade_no\":\"202310100011601002\"," +
////                "    \"total_amount\":100000," +
////                "    \"subject\":\"Iphone6 16G\"," +
////                "    \"product_code\":\"QUICK_WAP_WAY\"" +
////                "  }");//填充业务参数
////        String form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
////        httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
////        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
////        httpResponse.getWriter().flush();
////    }
//
//
//    /**
//     * 查询交易
//     * @return
//     * @throws AlipayApiException
//     */
//    @ResponseBody
//    @RequestMapping("/query")
//    public String queryPayResult() throws AlipayApiException {
//        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, APP_ID, APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE); //获得初始化的AlipayClient
//        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", "20210817010101004");  //支付时传入的商户订单号
////        bizContent.put("trade_no", "2014112611001004680073956707");    //支付时返回的支付宝交易号，与 out_trade_no 必填一个。
//        request.setBizContent(bizContent.toString());
//        AlipayTradeQueryResponse response = alipayClient.execute(request);
//        Map alipay_trade_query_response=null;
//        if (response.isSuccess()) {
//            System.out.println("调用成功");
//            String resultJson = response.getBody();
//            //转map
//            Map resultMap = JSON.parseObject(resultJson, Map.class);
//            alipay_trade_query_response = (Map) resultMap.get("alipay_trade_query_response");
//            //支付结果
//            System.out.println(alipay_trade_query_response);
//        } else {
//            System.out.println("调用失败");
//        }
//        return  JSON.toJSONString(alipay_trade_query_response)  + "\n" +alipay_trade_query_response.get("trade_status");
//    }
//
//
////    {
////        "alipay_trade_query_response": {
////        "code": "10000",
////                "msg": "Success",
////                "trade_no": "2013112011001004330000121536",
////                "out_trade_no": "6823789339978248",
////                "buyer_open_id": "01501o8f93I3nJAGB1jG4ONxtxV25DCN3Gec3uggnC4CJU0",
////                "buyer_logon_id": "159****5620",
////                "trade_status": "TRADE_CLOSED",
////                "total_amount": "88.88",
////                "trans_currency": "TWD",
////                "settle_currency": "USD",
////                "settle_amount": 2.96,
////                "pay_currency": 1,
////                "pay_amount": "8.88",
////                "settle_trans_rate": "30.025",
////                "trans_pay_rate": "0.264",
////                "alipay_store_id": "2015040900077001000100001232",
////                "buyer_pay_amount": "8.88",
////                "point_amount": "10",
////                "invoice_amount": "12.11",
////                "send_pay_date": "2014-11-27 15:45:57",
////                "receipt_amount": "15.25",
////                "store_id": "NJ_S_001",
////                "terminal_id": "NJ_T_001",
////                "fund_bill_list": [
////        {
////            "fund_channel": "ALIPAYACCOUNT",
////                "bank_code": "CEB",
////                "amount": 10,
////                "real_amount": 11.21,
////                "fund_type": "DEBIT_CARD"
////        }
////        ],
////        "store_name": "证大五道口店",
////                "buyer_user_id": "2088101117955611",
////                "discount_goods_detail": "[{\"goods_id\":\"STANDARD1026181538\",\"goods_name\":\"雪碧\",\"discount_amount\":\"100.00\",\"voucher_id\":\"2015102600073002039000002D5O\"}]",
////                "industry_sepc_detail": "{\"registration_order_pay\":{\"brlx\":\"1\",\"cblx\":\"1\"}}",
////                "industry_sepc_detail_gov": "{\"registration_order_pay\":{\"brlx\":\"1\",\"cblx\":\"1\"}}",
////                "industry_sepc_detail_acc": "{\"registration_order_pay\":{\"brlx\":\"1\",\"cblx\":\"1\"}}",
////                "voucher_detail_list": [
////        {
////            "id": "2015102600073002039000002D5O",
////                "name": "XX超市5折优惠",
////                "type": "ALIPAY_FIX_VOUCHER",
////                "amount": 10,
////                "merchant_contribute": 9,
////                "other_contribute": 1,
////                "memo": "学生专用优惠",
////                "template_id": "20171030000730015359000EMZP0",
////                "other_contribute_detail": [
////            {
////                "contribute_type": "BRAND",
////                    "contribute_amount": 8
////            }
////                ],
////            "purchase_buyer_contribute": 2.01,
////                "purchase_merchant_contribute": 1.03,
////                "purchase_ant_contribute": 0.82
////        }
////        ],
////        "charge_amount": "8.88",
////                "charge_flags": "bluesea_1",
////                "settlement_id": "2018101610032004620239146945",
////                "trade_settle_info": {
////            "trade_settle_detail_list": [
////            {
////                "operation_type": "replenish",
////                    "operation_serial_no": "2321232323232",
////                    "operation_dt": "2019-05-16 09:59:17",
////                    "trans_out": "208811****111111",
////                    "trans_in": "208811****111111",
////                    "amount": 10,
////                    "ori_trans_out": "2088111111111111",
////                    "ori_trans_in": "2088111111111111"
////            }
////            ]
////        },
////        "auth_trade_pay_mode": "CREDIT_PREAUTH_PAY",
////                "buyer_user_type": "PRIVATE",
////                "mdiscount_amount": "88.88",
////                "discount_amount": "88.88",
////                "buyer_user_name": "菜鸟网络有限公司",
////                "subject": "Iphone6 16G",
////                "body": "Iphone6 16G",
////                "alipay_sub_merchant_id": "2088301372182171",
////                "ext_infos": "{\"action\":\"cancel\"}",
////                "passback_params": "merchantBizType%3d3C%26merchantBizNo%3d2016010101111",
////                "hb_fq_pay_info": {
////            "user_install_num": "3",
////                    "fq_amount": "10.05"
////        },
////        "receipt_currency_type": "DC",
////                "credit_pay_mode": "creditAdvanceV2",
////                "credit_biz_order_id": "ZMCB99202103310000450000041833",
////                "enterprise_pay_info": {
////            "is_use_enterprise_pay": false,
////                    "invoice_amount": 80,
////                    "biz_info": "{\\\"enterprisePayAmount\\\":\\\"0.64\\\"}"
////        },
////        "hyb_amount": "10.24",
////                "bkagent_resp_info": {
////            "bindtrx_id": "123412341234",
////                    "bindclrissr_id": "01",
////                    "bindpyeracctbk_id": "123123123123",
////                    "bkpyeruser_code": "123451234512345",
////                    "estter_location": "+37.28/-121.268"
////        },
////        "charge_info_list": [
////        {
////            "charge_fee": 0.01,
////                "original_charge_fee": 0.01,
////                "switch_fee_rate": "0.03",
////                "is_rating_on_trade_receiver": "Y",
////                "is_rating_on_switch": "Y",
////                "charge_type": "trade",
////                "sub_fee_detail_list": [
////            {
////                "charge_fee": 0.1,
////                    "original_charge_fee": 0.2,
////                    "switch_fee_rate": "0.03"
////            }
////                ]
////        }
////        ]
////    },
////        "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
////    }
//
//
//
//
//    /**
//     * 接收通知
//     * @param request
//     * @param response
//     * @throws IOException
//     * @throws AlipayApiException
//     */
//    @PostMapping("/paynotify")
//    public void paynotify(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException {
//        //获取支付宝POST过来反馈信息
//        Map<String,String> params = new HashMap<String,String>();
//        Map requestParams = request.getParameterMap();
//        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
//            String name = (String) iter.next();
//            String[] values = (String[]) requestParams.get(name);
//            String valueStr = "";
//            for (int i = 0; i < values.length; i++) {
//                valueStr = (i == values.length - 1) ? valueStr + values[i]
//                        : valueStr + values[i] + ",";
//            }
//            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
//            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
//            params.put(name, valueStr);
//        }
//
//        //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//
//        //计算得出通知验证结果
//        //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
//        boolean verify_result = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, "RSA2");
//
//        if(verify_result){//验证成功
//            //商户订单号
//            String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//            //支付宝交易号
//            String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
//            //交易状态
//            String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
//            System.out.println(trade_status);
//
//            if(trade_status.equals("TRADE_FINISHED")){
//                //判断该笔订单是否在商户网站中已经做过处理
//                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
//                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//                //如果有做过处理，不执行商户的业务程序
//
//                //注意：
//                //如果签约的是可退款协议，退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
//                //如果没有签约可退款协议，那么付款完成后，支付宝系统发送该交易状态通知。
//            } else if (trade_status.equals("TRADE_SUCCESS")) {
//                //判断该笔订单是否在商户网站中已经做过处理
//                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
//                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
//                //如果有做过处理，不执行商户的业务程序
//
//                //注意：
//                //如果签约的是可退款协议，那么付款完成后，支付宝系统发送该交易状态通知。
//            }
//            System.out.println("success");
//        }else{//验证失败
//            System.out.println("fail");
//        }
//    }
//
//
//
//
//
//    @ResponseBody
//    @RequestMapping("/test")
//    public String test(){
//        return "hello";
//    }
//
//
//}
